package com.wanmait.zaisousuo.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.zaisousuo.mapper.ArticleMapper;
import com.wanmait.zaisousuo.pojo.Article;
import com.wanmait.zaisousuo.pojo.GarbageType;
import com.wanmait.zaisousuo.pojo.Texture;
import com.wanmait.zaisousuo.service.ArticleService;
import com.wanmait.zaisousuo.service.GarbageService;
import com.wanmait.zaisousuo.util.Pager;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.wanmait.zaisousuo.mapper.GarbageMapper;
import com.wanmait.zaisousuo.pojo.Garbage;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class GarbageServiceImpl implements GarbageService {


    @Resource
    private GarbageMapper garbageMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Resource(name="garbageSolrClient")
    private HttpSolrClient client;

    //多条件查询
    @Cacheable(value = "GarbageCache",key = "'findAllGarbageByConditions'+#typeId+#textureId+#pageNum")
    @Override
    public PageInfo<Garbage> findAllGarbageByConditions(Integer typeId, Integer textureId, Integer pageNum) {
        int pageSize=3;
        PageHelper.startPage(pageNum,pageSize);
        List<Garbage> allGarbage=garbageMapper.findAllGarbageByConditions(typeId,textureId);
        PageInfo pageInfo = new PageInfo<>(allGarbage);
        return pageInfo;
    }

    //。。。。。。。。。。。。。。。。。
    @Override
    public PageInfo<Article> findAllByConditions(Integer typeId, Integer textId, String orderBy, Integer pageNum) {
        int pageSize=10;
        PageHelper.startPage(pageNum,pageSize);
        List<Article> articles = articleMapper.findAllByConditions(typeId,textId,orderBy);
        PageInfo pageInfo = new PageInfo<>(articles);
        return pageInfo;
    }

    //根据审核状态找垃圾
    @Cacheable(value = "GarbageCache",key = "'findAllGarbageByState'+#state")
    @Override
    public List<Garbage> findAllGarbageByState(Integer state) {
        return garbageMapper.findAllGarbageByState(state);
    }

    //减少一条处理方式
    @Override
    public Integer updateReduceCountById(Integer id) {
        garbageMapper.updateReduceCountById(id);
        return id;
    }


    //通过用户id找垃圾
    @Cacheable(value = "GarbageCache",key = "'findByUserId'+#id")
    @Override
    public List<Garbage> findByUserId(Integer id) {
        return garbageMapper.findById(id);
    }

    //改变审核状态
    @Override
    public Boolean changeState(Boolean flag, Integer id) {
        garbageMapper.updateState(flag,id);
        return flag;
    }

    //增加处理方式数量
    @Override
    public void updateCountById(Integer id) {
        garbageMapper.updateCountById(id);
    }

    //添加垃圾
    @Override
    public void insert(Garbage garbage) {
      garbageMapper.insert(garbage);
    }

    //更改垃圾信息
    @Override
    public void update(Garbage garbage) {
      garbageMapper.updateByPrimaryKeySelective(garbage);
    }

    //假删除垃圾
    @Override
    public void delete(Integer id) {
     garbageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void delete(String ids) {

    }

    //通过id查询垃圾
    @Cacheable(value = "GarbageCache",key = "'findById'+#id")
    @Override
    public Garbage findById(Integer id) {
        return  garbageMapper.selectByPrimaryKey(id);
    }

    //查询所有垃圾
    @Cacheable(value = "GarbageCache",key = "'findAll'")
    @Override
    public List<Garbage> findAll() {
        return garbageMapper.findAll();
    }

    @Override
    public List<Garbage> findAll(Garbage object) {
        return null;
    }

    @Override
    public int getDataCount() {
        return 0;
    }

    @Override
    public int getDataCount(Garbage object) {
        return 0;
    }

    @Override
    public PageInfo<Garbage> findList(int pageNum) {
        return null;
    }

    //清除ehcache缓存
    @CacheEvict(value = "GarbageCache",allEntries = true)
    @Override
    public void clearCache() {
    }

    @Override
    public Integer getDataCount(String key, Integer pageNum) {
        Integer dataCount=null;
        SolrQuery query=new SolrQuery();
        query.setQuery("garbageTypeTitle:"+key +" or textureTitle:"+key+" or name:"+key);
//                or textureTitle:"+key+" or garbageTypeTitle:"+key
        QueryResponse response=null;
        try {
            response=client.query(query);
            SolrDocumentList documentList=response.getResults();
            dataCount=(int)documentList.getNumFound();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataCount;
    }

    //通过solr查询
    @Override
    public List<Garbage> findAllGarbageBySolr(String key, Integer pageNum, Pager pager) {
        SolrQuery query=new SolrQuery();
        //qurey设置查询的内容，显示效果，分页
        query.setQuery("garbageTypeTitle:"+key+" or textureTitle:"+key+" or name:"+key);
       //设置查询条件
        query.setHighlight(true);//开启高亮
        query.set("hl.fl","name or garbageTypeTitle or textureTitle");
        query.setStart((pager.getPageNum()-1)*pager.getPageSize());//当前页码减一，乘一页的数量，起始页
        query.setRows(pager.getPageSize());//结束，以pageSize

        //将查询的结果放在garbage对象中
        QueryResponse response=null;
        List<Garbage> allGarbage=new ArrayList<>();
        Garbage garbage=null;
        try {
            response=client.query(query);
            SolrDocumentList documentList=response.getResults();
            Map<String,Map<String,List<String>>> highLighting=response.getHighlighting();
            System.out.println(documentList.size()+"djfnjsnd");
            for(SolrDocument entries:documentList)
            {
                garbage=new Garbage();
                garbage.setPicture((String)entries.getFieldValue("picture"));
                garbage.setId(Integer.parseInt((String) entries.getFieldValue("id")));
                String id = (String) entries.getFieldValue("id");
                if(highLighting.get(id).get("name")==null){
                    garbage.setName((String)entries.getFieldValue("name"));
                }
                else{
                    garbage.setName(highLighting.get(id).get("name").get(0));
                }
                Texture texture=new Texture();
                texture.setId((Integer)entries.getFieldValue("textureId"));
                if(highLighting.get(id).get("textureTitle")==null){
                    texture.setTitle((String)entries.getFieldValue("textureTitle"));
                }
                else{
                    texture.setTitle(highLighting.get(id).get("textureTitle").get(0));
                }
                texture.setInco((String)entries.getFieldValue("textureInco"));
                garbage.setTexture(texture);

                GarbageType garbageType=new GarbageType();
                if(highLighting.get(id).get("garbageTypeTitle")==null){
                    garbageType.setTitle((String)entries.getFieldValue("garbageTypeTitle"));
                }
                else{
                    garbageType.setTitle(highLighting.get(id).get("garbageTypeTitle").get(0));
                }
                garbageType.setInco((String)entries.getFieldValue("garbageTypeInco"));
                garbageType.setId((Integer)entries.getFieldValue("garbageTypeId"));
                garbage.setGarbageType(garbageType);
                allGarbage.add(garbage);
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return allGarbage;
    }

    @Override
    public List<Garbage> findGarbageByName(String name) {
        return garbageMapper.findGarbageByName(name);
    }
}
